package com.cskaoyan.day4._02_arraylist;

import java.util.ArrayList;

/**
 * @author Common-zhou
 * @since 2023-03-02 10:15
 */
public class Demo2 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();




        //testClone();

        testClone2();
        testClone3();

    }

    private static void testClone3() {
        //    Object clone()
        //    返回此 ArrayList 实例的浅表副本。

        ArrayList<String> list = new ArrayList<>();
        list.add("zs");
        list.add("ls");
        list.add("wu");

        ArrayList<User> clone = (ArrayList<User>) list.clone();

        list.set(0, "Java");

        // zs  ls  wu
        System.out.println(clone);

        // Java   ls  wu
        System.out.println(list);
    }

    private static void testClone2() {
        //    Object clone()
        //    返回此 ArrayList 实例的浅表副本。

        ArrayList<User> list = new ArrayList<>();
        list.add(new User("zs"));
        list.add(new User("ls"));
        list.add(new User("wu"));

        ArrayList<User> clone = (ArrayList<User>) list.clone();

        clone.add(new User("Java"));

        //list.remove(1);
        list.get(0).name = "zhangsan";

        // zs   zhangsan
        System.out.println(clone);
        // zhangsan
        System.out.println(list);


    }

    private static void testClone() {
        //    Object clone()
        //    返回此 ArrayList 实例的浅表副本。

        ArrayList<String> arrayList = new ArrayList<>();

        arrayList.add("zs");
        arrayList.add("ls");
        arrayList.add("wu");

        ArrayList<String> clone = (ArrayList<String>) arrayList.clone();
        System.out.println(clone);

        clone.add("Java");

        System.out.println(clone);
        System.out.println(arrayList);


    }
}


class User {
    String name;

    public User(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" + "name='" + name + '\'' + '}';
    }
}
